import { Model, DataTypes } from 'sequelize';
import { sequelize } from '../config/database';
import { Circle } from './Circle';
import User from './User';

export class CircleMember extends Model {
    public id!: string;
    public circleId!: string;
    public userId!: string;
    public role!: 'admin' | 'moderator' | 'member';
    public status!: 'pending' | 'active' | 'rejected';
    public readonly createdAt!: Date;
    public readonly updatedAt!: Date;
}

CircleMember.init(
    {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true,
        },
        circleId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: Circle,
                key: 'id',
            },
        },
        userId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: User,
                key: 'id',
            },
        },
        role: {
            type: DataTypes.ENUM('admin', 'moderator', 'member'),
            allowNull: false,
            defaultValue: 'member',
        },
        status: {
            type: DataTypes.ENUM('pending', 'active', 'rejected'),
            allowNull: false,
            defaultValue: 'pending',
        },
    },
    {
        sequelize,
        tableName: 'circle_members',
        timestamps: true,
        indexes: [
            {
                unique: true,
                fields: ['circleId', 'userId'],
            },
        ],
    }
);

export default CircleMember; 